package algorithm.BinaryTree;

/**
 * 这个类编写了二叉树的三种遍历
 * 有递归和非递归两种方式
 */
public class Order {

    /**
     * 定义二叉树结点
     */
    public static class TreeNode{
        public TreeNode left;
        public TreeNode right;
        public int value;

        public TreeNode(int val){
            this.value=val;
        }
    }

    public static void Do(TreeNode node){
        //用此方法代表对二叉树结点的某种操作
        System.out.println(node.value);
    }

    /**
     * 递归版的先序遍历：中左右
     * @param head
     */
    public static void RecursivePreOrder(TreeNode head){
        if(head==null) return ;
        Do(head);
        RecursivePreOrder(head.left);
        RecursivePreOrder(head.right);
    }

    /**
     * 中序遍历递归版
     * @param head
     */
    public static void RecursiveInOrder(TreeNode head){
        if(head==null) return ;
        RecursiveInOrder(head.left);
        Do(head);
        RecursiveInOrder(head.right);
    }

    /**
     * 后序遍历递归版
     * @param head
     */
    public static void RecursiveProOrder(TreeNode head){
        if(head==null) return ;
        RecursiveProOrder(head.left);
        RecursiveProOrder(head.right);
        Do(head);
    }


}
